package handel

import (
	"context"
	"errors"
	"fmt"
	pb "zc/rpc/api/video"
	"zc/rpc/ess"
	"zc/rpc/global"
	"zc/rpc/model/mysql"
)

// 定义空接口
type UserInfoService struct {
	pb.UnimplementedVideoServer
}

func (s *UserInfoService) VideoAdd(ctx context.Context, req *pb.VideoAddReq) (*pb.VideoAddResp, error) {
	stu := mysql.Video{
		VideoTitle:   req.VideoTitle,
		VideoName:    req.VideoName,
		VideoInfo:    req.VideoInfo,
		VideoAddress: req.VideoAddress,
		VideoProfile: req.VideoProfile,
	}
	err := global.DB.Create(&stu).Error
	if err != nil {
		return nil, errors.New("添加失败")
	}
	fmt.Println("添加成功")
	return &pb.VideoAddResp{
		VideoId: int64(stu.ID),
	}, nil
}
func (s *UserInfoService) VideoList(ctx context.Context, req *pb.VideoListRequest) (*pb.VideoListResponse, error) {
	video := ess.EsVideo(req.VideoTitle)

	var data []*pb.VideoList
	for _, v := range video {
		data = append(data, &pb.VideoList{
			VideoTitle:   v.VideoTitle,
			VideoName:    v.VideoName,
			VideoInfo:    v.VideoInfo,
			VideoAddress: v.VideoAddress,
			VideoProfile: v.VideoProfile,
		})
	}
	fmt.Println(data)
	return &pb.VideoListResponse{VideoList: data}, nil
}
